/**  
* @Title: SysController.java
* @Package com.lemon.controller.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月8日
* @version V1.0  
*/
package com.lemon.controller.cityRun;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.conf.redis.RedisUtil;
import com.lemon.model.cityRun.CrSysFeedBook;
import com.lemon.model.cityRun.CrSysLogs;
import com.lemon.model.cityRun.CrSysOperate;
import com.lemon.model.cityRun.CrSysProtocol;
import com.lemon.service.cityRun.CrShopUserService;
import com.lemon.service.cityRun.CrSysFeedBookService;
import com.lemon.service.cityRun.CrSysOperateService;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;
import com.lemon.utils.XMLUtils;

/**
 * <p>Title: SysController</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月8日
 * @版本 V 1.0 
 */
@RestController
@RequestMapping("/sys")
public class SysController {

	@Resource
	private CrSysFeedBookService feedService;

	@Resource
	private CrShopUserService uService;

	@Resource
	private CrSysOperateService operService;

	/**
	 * <b>Description:<blockquote>设置系统平台基本信息
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@RequestMapping(value = "/getSysSet" , method = RequestMethod.POST)
	public ReqResult getShopTranSet(HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String string = RedisUtil.get(RedisUtil.key_before + "setting");
			JSONObject js = ((JSONObject) JSONObject.parse(string)).getJSONObject("sets").getJSONObject("sysSet");
			reqResult.setReturnResult(js);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "设置系统平台基本信息");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 查询意见反馈列表
	 * </blockquote></b>
	 * 
	 * @Title: getSysFeedBack
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月18日
	 */
	@RequestMapping(value = "/getSysFeedBack" , method = RequestMethod.POST)
	public ReqResult getSysFeedBack(@ModelAttribute CrSysFeedBook feedBook , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			List<CrSysFeedBook> list = feedService.selectFeedBookListBySelective(feedBook);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询意见信息异常");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 修改意见反馈信息
	 * </blockquote></b>
	 * 
	 * @Title: updateSysFeedBack
	 * @param feedBook
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月18日
	 */
	@RequestMapping(value = "/updateSysFeedBack.do" , method = RequestMethod.POST)
	public ReqResult updateSysFeedBack(@ModelAttribute CrSysFeedBook feedBook , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			String ids = request.getParameter("ids");
			if (StringUtil.isNotBlank(ids)) {
				String[] split = ids.split(",");
				for (int i = 0; i < split.length; i++) {
					feedBook.setId(split[i]);
					feedBook.setStatus(0);
					feedService.updateFeedBookListBySelective(feedBook);
				}
			} else {
				feedService.updateFeedBookListBySelective(feedBook);
			}
			reqResult.setReturnResult(1);

		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return reqResult;
	}

	@RequestMapping(value = "/addSysFeedBack.do" , method = RequestMethod.POST)
	public ReqResult addSysFeedBack(@ModelAttribute CrSysFeedBook feedBook , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			feedBook.setStatus(1);
			int list = feedService.addFeedBookBySelective(feedBook);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return reqResult;
	}
	/**
	 * <b>Description:<blockquote> 设置系统平台基本信息
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@LoginRequired
	@RequestMapping(value = "/addSysSet.do" , method = RequestMethod.POST)
	public ReqResult addShopTranSet(HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String minAmount = request.getParameter("platPhone");
			String distance = request.getParameter("distance");
			String freeDay1 = request.getParameter("freeDay1");
			String freeDay2 = request.getParameter("freeDay2");
			String firstYear1 = request.getParameter("firstYear1");
			String firstYear2 = request.getParameter("firstYear2");
			String nextYear1 = request.getParameter("nextYear1");
			String nextYear2 = request.getParameter("nextYear2");

			String jsonString = XMLUtils.getXmlToJSONString(XMLUtils.setUrl);
			JSONObject parse = (JSONObject) JSONObject.parse(jsonString);
			JSONObject js = parse.getJSONObject("sets");
			JSONObject js2 = js.getJSONObject("sysSet");
			if (js2 == null) {
				js2 = new JSONObject();
			}
			js2.put("platPhone" , minAmount);
			js2.put("distance" , distance);
			js2.put("freeDay1" , freeDay1);
			js2.put("freeDay2" , freeDay2);
			js2.put("firstYear1" , firstYear1);
			js2.put("firstYear2" , firstYear2);
			js2.put("nextYear1" , nextYear1);
			js2.put("nextYear2" , nextYear2);
			js.put("sysSet" , js2);
			parse.put("sets" , js);
			RedisUtil.set(RedisUtil.key_before + "setting" , parse);
			XMLUtils.saveXml(parse.toJSONString() , XMLUtils.setUrl);
			reqResult.setReturnResult(1);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "设置系统平台基本信息");
		}
		return reqResult;
	}

	@RequestMapping(value = "/getSysLogsList" , method = RequestMethod.POST)
	public ReqResult addSysLogs(@ModelAttribute CrSysLogs logs , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			logs.setStatus(1);
			logs.setOrderBy(" order by create_time desc");
			List<CrSysLogs> list = uService.selectLogsListBySelective(logs);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return reqResult;
	}

	///////////////////////////////关于协议和说明/////////////////////////////////////////
	@RequestMapping(value = "/getSysProtocolList" , method = RequestMethod.POST)
	public ReqResult getSysProtocolList(@ModelAttribute CrSysProtocol pro , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			pro.setStatus(1);
			List<CrSysProtocol> list = feedService.selectProtocolListBySelective(pro);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return reqResult;
	}

	@RequestMapping(value = "/getSysProtocolById" , method = RequestMethod.POST)
	public ReqResult getSysProtocolById(@ModelAttribute CrSysProtocol pro , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrSysProtocol list = feedService.selectProtocolBySelective(pro);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return reqResult;
	}

	@LoginRequired
	@RequestMapping(value = "/addSysProtocol.do" , method = RequestMethod.POST)
	public ReqResult addSysProtocol(@ModelAttribute CrSysProtocol pro , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			pro.setStatus(1);
			int list = feedService.insertProtocolBySelective(pro);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return reqResult;
	}

	@LoginRequired
	@RequestMapping(value = "/updateSysProtocol.do" , method = RequestMethod.POST)
	public ReqResult updateSysProtocol(@ModelAttribute CrSysProtocol pro , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			int list = feedService.updateProtocolBySelective(pro);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return reqResult;
	}

	@LoginRequired
	@RequestMapping(value = "/getOperateInfo" , method = RequestMethod.POST)
	public ReqResult getOperateInfo(@ModelAttribute CrSysOperate operate , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrSysOperate list = operService.getOperateInfo();
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return reqResult;
	}

	@RequestMapping(value = "/getOperateInfo1")
	public ModelAndView getOperateInfo1(@ModelAttribute CrSysOperate operate , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrSysOperate list = operService.getOperateInfo();
			reqResult.setReturnResult(list);
			ModelAndView m = new ModelAndView();
			m.addObject("oper" , list);
			m.setViewName("../public/back/admin/sys/operate.html");
			return m;
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return null;
	}

	@LoginRequired
	@RequestMapping(value = "/updateOperateInfo" , method = RequestMethod.POST)
	public ReqResult updateOperateInfo(@ModelAttribute CrSysOperate operate , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			int list = operService.updateOperateInfo(operate);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "修改意见信息异常");
		}
		return reqResult;
	}

}
